package com.nfcprogram.blu;
import android.util.Log;

public class BluetoothReconnectThread extends Thread {
	
		private static final String PING_SERVER_THREAD = "BluetoothReconnectThread";
//		private static final int ERROR_COUNT_MAX = 3;
//		private static final int SUCCESS_COUNT_MAX = 6;
		private static final int SLEEPTIME = 30*1000;
		private static final boolean V = true;
		//private int countError, countSuccess;
		private boolean keepAlive;
		private final BluetoothService bluService;

        public BluetoothReconnectThread(BluetoothService bluService) {
        	super(PING_SERVER_THREAD);
			this.bluService = bluService;
            //countSuccess = countError = 0;
            keepAlive = true;
        }
        
        @Override
        public void run() {
        	if (V) Log.v(PING_SERVER_THREAD, "! BluetoothReconnectThread started");
        	try {
        		Thread.sleep(SLEEPTIME*2);
				while(keepAlive){
					if(bluService.isListening()){
						bluService.reconnect();
					}
					Thread.sleep(SLEEPTIME);
				}
        	} catch (InterruptedException e) {
				e.printStackTrace();
			}
        }
/*
		private void updateCountersForError() {
			countError++;
			if(countError >= ERROR_COUNT_MAX){
				ContextAwareManager.getInstance().wifiDisconnected();
				countError = 0;
				countSuccess = 0;
			}
		}

		private void updateCountersForSuccess() {
			 countSuccess++;
			if(countSuccess >= SUCCESS_COUNT_MAX){
				ContextAwareManager.getInstance().wifiReconnected();
				countError = 0;
				countSuccess = 0;
			}
		}
*/
		public void cancel() {
			keepAlive = false;
			this.interrupt();
		}
}